using Flow.Manage.Contracts;
using Flow.Manage.Service;
using Framework.EF.Context;
using Framework.Models;
using Framework.Models.Entities;
using Framework.Models.Request;
using Framework.Repository;
using Framework.Utility.Helper;
using System.Reflection;

namespace NUnitTest
{
    public class Tests
    {
        private IAccountContract _accountContract;
        private IUserRepository _userRepository;
        private IUnitOfWork _unitOfWork;

        [SetUp]
        public void Setup()
        {
            MyDBContext myDBContext = new MyDBContext();
            _unitOfWork = new UnitOfWork(myDBContext);
            _userRepository = new UserRepository(_unitOfWork);
            _accountContract = new AccountService(_userRepository);
        }

        [Test]
        public void Test1()
        {
            var param = new AdminRequest
            {
                UserName = "admin",
                Password = "123456",
            };
            _accountContract.AdminLogin(param);
            Assert.Pass();
        }

        [Test]
        public void Test2()
        {
            var param = TagState.No.ToString("D");

            var param2 = TagState.Ok.ToString("D");

            var list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);

            int sortId = 0;
            var a = list.Select(x => new
            {
                b = sortId++,
                c = x.ToString()
            });

            Assert.Pass();
        }

        [Test]
        public void Test3()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);

            var FieldInfoArr = typeof(Biz_LeaveTime).GetFields(BindingFlags.Instance |
                       BindingFlags.Static |
                       BindingFlags.NonPublic |
                       BindingFlags.Public);

            foreach (FieldInfo fi in FieldInfoArr)
            {
                var aaa = fi.Name;
                var bbb = fi.GetValue(fi.Name);
            }
            Assert.Pass();
        }

        [Test]
        public void Test4()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);
            var dic = new Dictionary<string, string>();

            foreach (PropertyInfo p in biz_LeaveTime.GetType().GetProperties())
            {
                var aaa = p.Name.ToString();
                var bbb = p.GetValue(biz_LeaveTime)?.ToString();
                if (!string.IsNullOrEmpty(bbb))
                {
                    dic.Add(aaa, bbb);
                }
            }

            var sss = dic.ToJson();

            var dic2 = new Dictionary<string, string>();

            var sss2 = dic2.ToJson();

            var frmDataJson = JsonHelper.IsJsonNullOrEmpty(sss2);

            Assert.Pass();
        }

        [Test]
        public void Test5()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);

            var name = typeof(Biz_LeaveTime);

            var aaaa = Activator.CreateInstance(name);

            Assert.Pass();
        }

        [Test]
        public void Test6()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);

            var _type = typeof(Biz_LeaveTime);

            var aaaa = Activator.CreateInstance(_type);

            var dic = new Dictionary<string, string>();

            foreach (PropertyInfo p in _type.GetProperties())
            {
                var aaa = p.Name.ToString();
                var bbb = p.GetValue(aaaa)?.ToString();
                if (!string.IsNullOrEmpty(bbb))
                {
                    dic.Add(aaa, bbb);
                }
            }

            var sss2 = dic.ToJson();

            Assert.Pass();
        }

        [Test]
        public void Test7()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);

            var aa = Insert(biz_LeaveTime);

            var bbb = Insert(biz_LeaveTime, it =>
            {
                it.LeaveHour = 20;
                it.StartDate = DateTime.Now.AddDays(10);
                it.EndDate = DateTime.Now.AddDays(10);
                return it;
            });
        }

        [Test]
        public void Test8()
        {
            Biz_LeaveTime biz_LeaveTime = new Biz_LeaveTime();
            biz_LeaveTime.LeaveHour = 16;
            biz_LeaveTime.StartDate = DateTime.Now;
            biz_LeaveTime.EndDate = DateTime.Now.AddDays(2);

            var aa = Insert2(biz_LeaveTime);

            var bbb = Insert2(biz_LeaveTime, it =>
            {
                it.LeaveHour = 20;
                it.StartDate = DateTime.Now.AddDays(10);
                it.EndDate = DateTime.Now.AddDays(10);
            });
        }

        private T Insert<T>(T parm, Func<T, T> iClumns = null, bool ignoreNull = true)
        {
            if (iClumns != null)
            {
                parm = iClumns(parm);
            }
            return parm;
        }

        private T Insert2<T>(T parm, Action<T> action = null, bool ignoreNull = true)
        {
            if (action != null)
            {
                action(parm);
            }

            return parm;
        }
    }
}